package Controller;

import repository.ReleaseRepository;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/NewsContent")
public class NewsContent extends HttpServlet{
    private static final long serialVersionUID = 1L;


    public NewsContent(){
        super();
        //TODO Auto-generated constructor stub
    }
    public String turn(String str) {

        while (str.indexOf("\n") != -1) {
            str = str.substring(0, str.indexOf("\n")) + "<br>"+ str.substring(str.indexOf("\n") + 1);
        }
        while (str.indexOf("\t") != -1) {
            str = str.substring(0, str.indexOf("\t")) + "&nbsp"+ str.substring(str.indexOf("\t") + 1);
        }

        return str;
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();
        String name =new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");
        String headline =new String(request.getParameter("headline").getBytes("ISO8859-1"),"UTF-8");
        String news_content =new String(request.getParameter("news-content").getBytes("ISO8859-1"),"UTF-8");

        String news_con = turn(news_content);
        System.out.println(name);
        System.out.println(headline);

//        String chkparm = list.get(position).getVar_chkparm().trim();
//        holder.item_chkparm.setText(chkparm.replaceAll("\r|\n*",""));

        news_con = news_con.replaceAll("\r|\n", "");
        news_con = news_con.replaceAll("\t| ", "");
        System.out.println("s="+news_con);
        //进入ReleaseRepository
        ReleaseRepository releaseRepository = new ReleaseRepository();
        boolean release = false;
        try {
            release = releaseRepository.ReleaseNews(headline,name,news_con);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        System.out.println("release="+release);

    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
